//
//  RCDepartureAndArriveView.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-3.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCDepartureAndArriveView.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"


@implementation RCDepartureAndArriveView{
    RCDepartureAndArriveViewType _type;
    UIImageView *leftIcon;
    UILabel *leftHeadLabel;
    UIImageView *leftFormBGView;
    
    UIButton *switchBtn;
    
    UIImageView *rightIcon;
    UILabel *rightHeadLabel;
    UIImageView *rightFormBGView;
    
    UIButton *leftBtn;
    UIButton *rightBtn;
}

@synthesize delegate = _delegate;
@synthesize leftValue = _leftValue;
@synthesize rightValue = _rightValue;

- (id)initWithFrame:(CGRect)frame andType:(RCDepartureAndArriveViewType)type
{
    self = [super initWithFrame:frame];
    if (self) {
        _type = type;
        // Initialization code
        [self createPlaceHolder];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void) createPlaceHolder
{
    leftIcon = [[UIImageView alloc] initWithFrame:CGRectMake(32, 0, 16, 16)];
    [self addSubview:leftIcon];
    if (_type == RCDepartureAndArriveViewType_RoundTripDes) {
        leftIcon.image = [UIImage imageNamed:@"flyTicket_DepartureIcon"];
    }else if (_type == RCDepartureAndArriveViewType_RoundTripTime ||_type == RCDepartureAndArriveViewType_SingleTripTime){
        leftIcon.image = [UIImage imageNamed:@"flyTicket_StartTimeIcon"];
    }else if (_type == RCDepartureAndArriveViewType_SeatTypeView){
        leftIcon.image = [UIImage imageNamed:@"flyTicket_CaptinTypeIcon"];
    }else if (_type == RCDepartureAndArriveViewType_TrainTypeView){
        leftIcon.image = [UIImage imageNamed:@"flyTicket_CaptinTypeIcon"];
    }
    [leftIcon release];
    
    leftHeadLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 100, 14)];
    [self addSubview:leftHeadLabel];
    leftHeadLabel.backgroundColor = [UIColor clearColor];
    leftHeadLabel.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    leftHeadLabel.textColor = kNormalTextColor;
    if (_type == RCDepartureAndArriveViewType_RoundTripDes) {
        leftHeadLabel.text = @"出发城市";
    }else if (_type == RCDepartureAndArriveViewType_RoundTripTime ||_type == RCDepartureAndArriveViewType_SingleTripTime){
        leftHeadLabel.text = @"出发日期";
    }else if (_type == RCDepartureAndArriveViewType_SeatTypeView){
        leftHeadLabel.text = @"舱位选择";
    }else if (_type == RCDepartureAndArriveViewType_TrainTypeView){
        leftHeadLabel.text = @"车次类型";
    }
    [leftHeadLabel release];
    
  
    
    if (_type == RCDepartureAndArriveViewType_SeatTypeView ||_type == RCDepartureAndArriveViewType_SingleTripTime) {
        leftFormBGView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 25, 280, 40)];

    }else{
        leftFormBGView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 25, 125, 40)];
    }
    leftFormBGView.image = [[UIImage imageNamed:@"flyTicket_form_bg"] stretchableImageWithLeftCapWidth:60 topCapHeight:20];
    [self addSubview:leftFormBGView];
    [leftFormBGView release];
 
    leftBtn = [[UIButton alloc] initWithFrame:leftFormBGView.frame];
    [self addSubview:leftBtn];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBtn setBackgroundColor:[UIColor clearColor]];
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn release];
    
    if (_type == RCDepartureAndArriveViewType_RoundTripDes) {
        switchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        switchBtn.frame = CGRectMake(145, 33, 25, 22);
        [switchBtn setBackgroundImage:[UIImage imageNamed:@"flyTicket_SwitchIcon"] forState:UIControlStateNormal];
        [self addSubview:switchBtn];
        [switchBtn addTarget:self action:@selector(switchLeftWithRight) forControlEvents:UIControlEventTouchUpInside];

    }
  
    
    if (_type == RCDepartureAndArriveViewType_SeatTypeView ||_type == RCDepartureAndArriveViewType_SingleTripTime) {
        return;
    }
    
    rightIcon = [[UIImageView alloc] initWithFrame:CGRectMake(32+160, 0, 16, 16)];
    [self addSubview:rightIcon];
    if (_type == RCDepartureAndArriveViewType_RoundTripDes) {
        rightIcon.image = [UIImage imageNamed:@"flyTicket_ArrivedIcon"];
    }else if (_type == RCDepartureAndArriveViewType_RoundTripTime){
        rightIcon.image = [UIImage imageNamed:@"flyTicket_EndTimeIcon"];
    }else if (_type == RCDepartureAndArriveViewType_TrainTypeView){
        rightIcon.image = [UIImage imageNamed:@"flyTicket_EndTimeIcon"];
    }
    [rightIcon release];
  
    
    rightHeadLabel = [[UILabel alloc] initWithFrame:CGRectMake(60+160, 0, 100, 14)];
    [self addSubview:rightHeadLabel];
    rightHeadLabel.backgroundColor = [UIColor clearColor];
    rightHeadLabel.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    rightHeadLabel.textColor = kNormalTextColor;
    if (_type == RCDepartureAndArriveViewType_RoundTripDes) {
        rightHeadLabel.text = @"到达城市";
    }else if (_type == RCDepartureAndArriveViewType_RoundTripTime){
        rightHeadLabel.text = @"返回日期";
    }else if (_type == RCDepartureAndArriveViewType_TrainTypeView){
        rightHeadLabel.text = @"座席类型";
    }
    [rightHeadLabel release];
    
    rightFormBGView = [[UIImageView alloc] initWithFrame:CGRectMake(15+160, 25, 125, 40)];
    rightFormBGView.image = [UIImage imageNamed:@"flyTicket_form_bg"];
    [self addSubview:rightFormBGView];
    [rightFormBGView release];
    
    rightBtn = [[UIButton alloc] initWithFrame:rightFormBGView.frame];
    [self addSubview:rightBtn];
    [rightBtn setBackgroundColor:[UIColor clearColor]];
    [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [rightBtn release];
}

- (void) changeToMode:(RCDepartureAndArriveViewType)type
{
    if (type == RCDepartureAndArriveViewType_SingleTripTime) {
        rightIcon.hidden = YES;
        rightHeadLabel.hidden = YES;
        rightFormBGView.hidden = YES;
        rightBtn.hidden = YES;
        leftFormBGView.frame = CGRectMake(15, 25, 280, 40);
        
    }else{
        rightIcon.hidden = NO;
        rightHeadLabel.hidden = NO;
        rightFormBGView.hidden = NO;
        rightBtn.hidden = NO;
        leftFormBGView.frame = CGRectMake(15, 25, 125, 40);
    }
    leftBtn.frame = leftFormBGView.frame;

}


- (void) leftBtnClick
{
    [_delegate rcDepartureAndArriveViewClickAtSide:0 withState:_type];

}

- (void) rightBtnClick
{
    [_delegate rcDepartureAndArriveViewClickAtSide:1 withState:_type];
}


- (void) setLeft:(NSString *)leftName withLeftID:(NSString *)leftID
{

    [leftBtn setTitle:leftName forState:UIControlStateNormal];
    self.leftValue = leftID;
    
}
- (void) setRight:(NSString *)rightName withRightID:(NSString *)rightID
{
    NSLog(@"%@",rightName);
    [rightBtn setTitle:rightName forState:UIControlStateNormal];
    self.rightValue = rightID;
    
}


- (NSString *)leftStr
{
    return leftBtn.titleLabel.text;
}


- (NSString *)rightStr
{
    return rightBtn.titleLabel.text;
}


- (void) switchLeftWithRight
{
    NSString *leftText =   leftBtn.titleLabel.text;
    NSString *rightText =   rightBtn.titleLabel.text;
    CGRect leftFrame = leftBtn.frame;
    CGRect rightFrame = rightBtn.frame;
    
    [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
        leftBtn.frame = rightFrame;
        rightBtn.frame = leftFrame;
    } completion:^(BOOL finished) {
        [leftBtn setTitle:rightText forState:UIControlStateNormal];
        [rightBtn setTitle:leftText forState:UIControlStateNormal];
        leftBtn.frame = leftFrame;
        rightBtn.frame = rightFrame;
        
        NSString *t = self.leftValue;
        self.leftValue = self.rightValue;
        self.rightValue = t;
    }];
    
    
    
}
@end
